package com.module.lemlin.owner

import android.content.pm.PackageManager
import android.content.res.Resources
import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.*
import com.module.lemlin.app.OwnerStatusBar

abstract class OwnerAbstractActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.doBeforeInit()
        this.initStatusBar()
        this.initViewMode()
        this.initView()
        this.initData()
    }

    override fun onStop() {
        super.onStop()
        //隐藏软键盘
        if (KeyboardUtils.isSoftInputVisible((this@OwnerAbstractActivity))) {
            KeyboardUtils.hideSoftInput((this@OwnerAbstractActivity))
        }
    }

    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    override fun getResources(): Resources {
        val resources = super.getResources()
        val newConfig = resources.configuration
        if (newConfig.fontScale != 1f) {
            newConfig.fontScale = 1f
            return createConfigurationContext(newConfig).resources
        }
        return super.getResources()
    }

    /**
     * 初始化操作
     * */
    private fun init() {
        this.doBeforeInit()
        this.initStatusBar()
        this.initViewMode()
        this.initView()
        this.initData()
    }

    /**
     * 状态栏设置
     * */
    private fun setStatusBar(statusBar: OwnerStatusBar = OwnerStatusBar()) {
        BarUtils.setStatusBarLightMode(
            window, statusBar.lightMode
        )
        BarUtils.setStatusBarColor(
            window, ColorUtils.getColor(statusBar.barColor)
        )
        BarUtils.setNavBarColor(
            window, ColorUtils.getColor(statusBar.navColor)
        )
        statusBar.addView?.let {
            BarUtils.addMarginTopEqualStatusBarHeight(it)
        }
    }

    /**
     * 请求权限结果
     */
    override fun onRequestPermissionsResult(
        requestCode: Int, permissions: Array<out String>, grantResults: IntArray,
    ) {
        var granted = true
        for (result in grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                granted = false
            }
        }
        if (!handlePermissionResult(requestCode, granted)) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        }
    }

    /**
     * 检查是否已被授权危险权限
     */
    private fun checkDangerousPermissions(permissions: Array<String>): Boolean {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true
        }
        for (permission in permissions) {
            if (ContextCompat.checkSelfPermission(this,
                    permission) != PackageManager.PERMISSION_GRANTED ||
                ActivityCompat.shouldShowRequestPermissionRationale(this, permission)
            ) {
                return false
            }
        }
        return true
    }

    /**
     * 请求权限
     */
    open fun requestDangerousPermissions(permissions: Array<String>, requestCode: Int) {
        if (checkDangerousPermissions(permissions)) {
            handlePermissionResult(requestCode = requestCode, granted = true)
            return
        }
        ActivityCompat.requestPermissions(this, permissions, requestCode)
    }

    /**
     * 处理请求危险权限的结果
     */
    open fun handlePermissionResult(requestCode: Int, granted: Boolean): Boolean {
        return false
    }

    /**
     * 初始化视图和数据之前进行操作
     */
    open fun doBeforeInit() = Unit

    /**
     * 初始化状态栏
     * */
    open fun initStatusBar(statusBar: OwnerStatusBar = OwnerStatusBar()) =
        setStatusBar(statusBar)

    /**
     * 初始化 view mode
     * */
    open fun initViewMode() = Unit

    /**
     * 初始化视图
     */
    open fun initView() = Unit

    /**
     * 初始化数据
     */
    open fun initData() = Unit

    /**
     * toast提示
     * */
    fun toast(message: String?) {
        ToastUtils.showShort(message)
    }

    /**
     * debug logger
     * */
    fun debugLog(any: Any) {
        LogUtils.e(javaClass.name, any)
    }
}